package com.ls.config;

import com.ls.quartz.AlphaJob;
import com.ls.quartz.PostScoreRefreshJob;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

/**
 * 分布式定时任务 Spring Quartz
 * 配置初始化到数据库
 * 后面访问数据库
 *
 * @author ls
 * @version v1.0
 * @description
 * @date 2021/8/24 21:34
 */
@Configuration
public class QuartzConfig {

    /**
     * 功能描述: <br>
     * FactoryBean可简化Bean的实例化过程：
     * 1.通过FactoryBean封装Bean的实例化过程
     * 2.将FactoryBean装配到容器中
     * 3.将FactoryBean注入给其他的Bean
     * 4.该Bean得到的是FactoryBean所管理的对象示例
     * <p>
     * 配置JobDetail 任务详情
     *
     * @return {@link JobDetailFactoryBean}
     * @author ls
     * @date 2021/8/24 21:37
     */
    // @Bean
    public JobDetailFactoryBean alphaJobDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(AlphaJob.class);
        factoryBean.setName("alphaJob");
        factoryBean.setGroup("alphaJobGroup");
        // 指定作业的持久性，即即使没有触发器再指向它，它是否仍应保存在作业存储中
        factoryBean.setDurability(true);
        // 设置此作业的恢复标志，即如果遇到“恢复”或“故障转移”情况，是否应重新执行作业
        factoryBean.setRequestsRecovery(true);
        return factoryBean;
    }

    /**
     * 功能描述: <br>
     * 配置 Trigger 触发器
     * SimpleTriggerFactoryBean, CronTriggerFactoryBean
     *
     * @param alphaJobDetail
     * @return {@link SimpleTriggerFactoryBean}
     * @author ls
     * @date 2021/8/24 21:40
     */
    // @Bean
    public SimpleTriggerFactoryBean alphaTrigger(JobDetail alphaJobDetail) {
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(alphaJobDetail);
        factoryBean.setName("alphaTrigger");
        factoryBean.setGroup("alphaTriggerGroup");
        // 指定此触发器的执行时间间隔。
        factoryBean.setRepeatInterval(3000);
        factoryBean.setJobDataMap(new JobDataMap());
        return factoryBean;
    }

    /**
     * 功能描述: <br>
     *
     * @return {@link JobDetailFactoryBean}
     * @author ls
     * @date 2021/8/24 23:32
     */
    @Bean
    public JobDetailFactoryBean postScoreRefreshJobDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(PostScoreRefreshJob.class);
        factoryBean.setName("postScoreRefreshJob");
        factoryBean.setGroup("communityJobGroup");
        // 指定作业的持久性，即即使没有触发器再指向它，它是否仍应保存在作业存储中
        factoryBean.setDurability(true);
        // 设置此作业的恢复标志，即如果遇到“恢复”或“故障转移”情况，是否应重新执行作业
        factoryBean.setRequestsRecovery(true);
        return factoryBean;
    }

    @Bean
    public SimpleTriggerFactoryBean postScoreRefreshTrigger(JobDetail postScoreRefreshJobDetail) {
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(postScoreRefreshJobDetail);
        factoryBean.setName("postScoreRefreshTrigger");
        factoryBean.setGroup("communityTriggerGroup");
        // 指定此触发器的执行时间间隔。 5分钟一次
        factoryBean.setRepeatInterval(1000 * 60 * 5L);
        factoryBean.setJobDataMap(new JobDataMap());
        return factoryBean;
    }
}
